<?php
/**
 * CpController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Admin_HoSoController extends Sunnet_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	$this->view->arrParam = $this->_arrParam;
    	if (empty($this->_arrParam['page'])) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (empty($this->_arrParam['line_per_page'])) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
        Zend_Loader::loadClass('Admin_Models_MenuModel');
         $db = new Admin_Models_MenuModel();
         $mainmenu = $db->loadmainmenu();
         $this->view->mainmenu = $mainmenu; 
         
         function vn_str_filter ($str){
			$unicode = array(
					'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
					'd'=>'đ',
					'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
					'i'=>'í|ì|ỉ|ĩ|ị',
					'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
					'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
					'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
					'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
					'D'=>'Đ',
					'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
					'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
					'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
					'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
					'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
					'_'=>' ',
			);
		
			foreach($unicode as $nonUnicode=>$uni){
				$str = preg_replace("/($uni)/i", $nonUnicode, $str);
			}
			return $str;
		}
    }

    public function preDispatch(){
        $this->view->headScript()->appendFile('/js/ckeditor/ckeditor.js','text/javascript');
         $template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');           
    	$auth = Zend_Auth::getInstance();
        $this->view->userName = $auth->getIdentity()->username;
    	if ((!$auth->hasIdentity() || !$auth->getIdentity()->admin) && ($this->_arrParam['action'] != 'login')) {
    		$this->_forward('login','index','admin',array('action'	=> 'login'));//,$this->_arrParam);
    	}
        $this->view->headScript()->appendFile('/js/ckeditor/ckeditor.js','text/javascript');
        $this->view->headScript()->appendFile('/js/ckeditor/styles.js','text/javascript');
        $this->view->headScript()->appendFile('/js/ckeditor/ckeditor.css','text/css');
    }
   
    public function indexAction(){
         Zend_loader::loadClass('Admin_Models_HoSoModel');
//		Load các model và form sử dụng trong action
         $db = new Admin_Models_HoSoModel();
         $listpage = $db->listpage();
         $currentPage = 1;		 
		$i = $this->_getParam('page',1);
		if(!empty($i))
		{
			$currentPage = $i;
		}
		$paginator = Zend_Paginator::factory($listpage);
		$paginator->setItemCountPerPage(10)
		->setPageRange(4)
		->setCurrentPageNumber($currentPage);
		$this->view->paginator = $paginator;                 
    }


    public function editPageAction(){

         $id = $this->_request->getParam('id');

        if(intval($id)>0){ 

	        Zend_loader::loadClass('Admin_Forms_HoSoForm');
	        Zend_loader::loadClass('Admin_Models_HoSoModel');
			$session = new Zend_Session_Namespace();
	        $form = new Admin_Forms_HoSoForm(null);

			$db = new Admin_Models_HoSoModel();
			//load ho so
	        $result_edit = $db->loadpagebyid($id);
	        $this->view->result_edit = $result_edit;

	        ////////////////////
	        
			$form->setDefaults($result_edit);
	  		$this->view->form=$form;

	        $request=$this->getRequest();
	        	if($request->isPost()){

					try {
								$data= array(
											'giatrikhoanvay_ad'=> $this->_request->getParam('giatrikhoanvay_ad'),
											'thoigianvay_ad'=>$this->_request->getParam('thoigianvay_ad'),
											'taisanthechap_ad'=>$this->_request->getParam('taisanthechap_ad'),												
											'mucdichsudungvon_ad'=>$this->_request->getParam('mucdichsudungvon_ad'),

											'nganhangtiepnhan'=>$this->_request->getParam('nganhangtiepnhan'),
											'capnhatlaisuat'=>$this->_request->getParam('capnhatlaisuat'),
											'tinhtranghoso'=>$this->_request->getParam('tinhtranghoso'),
											'thoigiangiaingan'=>$this->_request->getParam('thoigiangiaingan'),

											'view'			=> 1,
											'active'		=>$this->_request->getParam('active')
								);
								$result = $db->updatepage($data,$id);

							if($result>0){
								// <script>alert('Page added');
								echo "<script>window.location = '/admin/ho-so/';</script>";		   				
							}else {
								echo "<script>alert('Add Fail');window.location = '/admin/ho-so/';</script>";
							}
							
					}catch (Exception $e) {
						echo $e;
					}	
									
				} // if request

		} // neu ton tai id
    }
    
    public function delPageAction(){

       $id = $this->_request->getParam('id');
       Zend_loader::loadclass('Admin_Models_PageModel');

       $db_del = new Admin_Models_PageModel();      
       $result = $db_del->delpage($id);
       
       if($result>0){          
       		//alert('Xóa bài viết thành công ');  
			echo "<script>window.location = '/admin/page';</script>";
		}else {		
			echo "<script>alert('Xóa bài viết thất bại');window.location = '/admin/page';</script>";
		}
    }
	
}
